package com.jc.camerascan.activity.webview;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.jc.camerascan.R;
import com.jc.camerascan.core.BaseActivity;
import com.jc.camerascan.utils.MMKVUtils;


import com.just.agentweb.core.AgentWeb;
import com.tencent.wxop.stat.StatService;
import com.xuexiang.xui.widget.actionbar.TitleBar;

import butterknife.BindView;

/**
 * @Package: com.jc.camerascan.activity
 * @Description: java类作用描述
 * @Author: xuchi
 * @CreateDate: 2020/7/30 2:10
 * @Version: 1.0
 */
public class WebviewActivity extends BaseActivity {


    @BindView(R.id.titlebar)
    TitleBar titlebar;
    @BindView(R.id.container)
    LinearLayout container;

    private AgentWeb mAgentWeb;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    public void initView() {
        Intent intent = getIntent();
        String url = intent.getExtras().getString("url");
        String titlestr = intent.getExtras().getString("title");
        boolean isNeedToken = intent.getExtras().getBoolean("token", true);
        titlebar.setTitle(titlestr + "");
        titlebar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mAgentWeb != null) {
                    if (!mAgentWeb.back()) {
                        WebviewActivity.this.finish();
                    } else {
                        mAgentWeb.back();
                    }
                }
            }
        });

        if (isNeedToken) {
            if (url.toLowerCase().contains("?")) {
                url = url + "&token=" + MMKVUtils.getString("xc_access_token", "");
            } else {
                url = url + "?token=" + MMKVUtils.getString("xc_access_token", "");
            }
        }
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(container, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .createAgentWeb()
                .ready()
                .go(url);

        mAgentWeb.getJsInterfaceHolder().addJavaObject("ztfunc", new AndroidInterfaceWeb(mAgentWeb, this));
    }


    @Override
    protected void onResume() {
        super.onResume();
        StatService.onResume(this);
        if (mAgentWeb != null) {
            mAgentWeb.getWebLifeCycle().onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPause(this);
        if (mAgentWeb != null) {
            mAgentWeb.getWebLifeCycle().onPause();
        }
    }

    @Override
    protected void onDestroy() {
        if (mAgentWeb != null) {
            mAgentWeb.getWebLifeCycle().onDestroy();
        }
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mAgentWeb != null) {
            if (mAgentWeb.handleKeyEvent(keyCode, event)) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}
